package com.cheyun.netsalev3.view

import android.os.Bundle
import android.widget.LinearLayout
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.utils.BaseActivity
import com.just.agentweb.AgentWeb
import android.text.TextUtils
import android.webkit.WebView
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiStores
import com.just.agentweb.WebChromeClient
import java.net.URLEncoder
import android.graphics.Bitmap
import com.cheyun.netsalev3.databinding.ActivityDriveBlockBinding
import com.just.agentweb.WebViewClient


class DriveBlockActivity : BaseActivity<ActivityDriveBlockBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_drive_block
    }

    var type = ""
    var url = ""
    override fun initView() {

        var cauthkey = MySharedPreferences.getCauthkey()
        cauthkey = URLEncoder.encode(cauthkey)

        type = intent.getStringExtra("type")
        if(type.equals("DriveBlock")){
            url = "drivelist?cauthkey="+cauthkey
        }else if(type.equals("orderMap")){
            var tid = intent.getStringExtra("tid")
            var channel = intent.getStringExtra("channel")
            var id = intent.getStringExtra("id")
            url = "qqmap?tid="+tid+"&channel="+channel+"&id="+id
        }
    }

    override fun initData() {

        DemoLog.i("DriveBlockActivity URL",ApiStores.ANALYSIS_URL+url)

        var mAgentWeb = AgentWeb.with(this)//传入Activity
                .setAgentWebParent(binding.llWebview,  LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go( ApiStores.ANALYSIS_URL+"/"+url)


        mAgentWeb.webCreator.webView.setWebChromeClient(object : WebChromeClient() {
             override fun onReceivedTitle(view: WebView, title: String) {
                if (!TextUtils.isEmpty(title)) {
                    binding.headview.titleText?.text = title
                }
                super.onReceivedTitle(view, title)
            }

            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                DemoLog.i("DriveBlockActivity","newProgress:"+newProgress)
                mAgentWeb.indicatorController.setProgress(newProgress)
            }
        })
    }

    private val mWebViewClient = object : WebViewClient() {
        override  fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
            DemoLog.i("DriveBlockActivity","url:"+url)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

}
